//
//  SAQuestion.m
//  GuessImage
//
//  Created by 解晓东 on 15/11/18.
//  Copyright (c) 2015年 sasa. All rights reserved.
//

#import "SAQuestion.h"

@implementation SAQuestion

- (instancetype)initWithDict:(NSDictionary *)dict {
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
        
        // 对备选按钮进行乱序，只在加载的时候做一次乱序
        [self randamOptions];
    }
    
    return self;
}

+ (instancetype)questionWithDict:(NSDictionary *)dict {
    return [[self alloc] initWithDict:dict];
}

+ (NSArray *)questionList {
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]];
    
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self questionWithDict:dict]];
    }
    
    return arrayM;
}

- (void)randamOptions {
    // 对options数组乱序
    self.options = [self.options sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        int seed = arc4random_uniform(2);
        
        if (seed) {
            return [obj1 compare:obj2];
        } else {
            return [obj2 compare:obj1];
        }
    }];
}

// 对象描述方法，便于跟踪调试
- (NSString *)description {
    return [NSString stringWithFormat:@"<%@:%p> {answer:%@, icon:%@, title:%@, options:%@}", self.class, self, self.answer, self.icon, self.title, self.options];
}

@end
